(function(){
	window.System=arguments.callee;
	var Me=System;
	Me.param={};
	Me.config={};
	window.int=function(v){
		var n=v*1;
		if(isNaN(n)) return 0;
		if(n>=0) return Math.floor(n);
		return Math.ceil(n);
	}
	window.is=function(classObj,obj){
		if(obj==null) return false;
		if(classObj==String) return typeof(obj)=="string";
		if(classObj==Number) return typeof(obj)=="number";
		if(!classObj.__isClass) return obj instanceof classObj;
		var classObj2=obj.__class;
		while(classObj2){
			if(classObj2==classObj) return true;
			classObj2=classObj2.__super;
		}
		return false;
	}
	window.call=function(foo,v1,v2,v3){
		if (!foo) return null;
		if(v1!=null){
			foo=foo.slice(0);
			var n=typeof(foo[1])=="function"?2:1;
			foo.splice(n,0,v1);
			if(v2!=null) foo.splice(n+1,0,v2);
			if(v3!=null) foo.splice(n+2,0,v3);
		}
		if(typeof(foo)=="function"){
			return foo.apply();
		}
		if(typeof(foo[0])=="function"&&!foo[0].__isClass){
			return foo[0].apply(null, foo.slice(1));
		}
		else if(foo[0].__isClass){
			return Class.new2.apply(null,foo);
		}
		else if(typeof(foo[1])=="function") {
			return foo[1].apply(foo[0], foo.slice(2));
		}
	}
	window.callLater=function(foo,time){
		if(!foo) return;
		if(time==null) time=1;
		var existObj;
		for(var i=0;i<GAid.laterCalls.length;i++){
			var obj=GAid.laterCalls[i];
			if(is(Function,foo[0])){
				if(foo[0]==obj.foo[0]) existObj=obj;
			}
			else{
				if(foo[0]==obj.foo[0]&&foo[1]==obj.foo[1]) existObj=obj;
			}			
		}
		if(existObj){
			existObj.time=time;
			existObj.status=1;
		}
		else{
			GAid.laterCalls.push({foo:foo,time:time,status:1});
		}
	}
	window.clearCall=function(caller,func){
		for(var i=0;i<GAid.laterCalls.length;i++){
			var obj=GAid.laterCalls[i];
			if(is(Function,obj.foo[0])){
				if(caller==obj.foo[0]) obj.status=2;
			}
			else{
				if(caller==obj.foo[0]&&func==obj.foo[1]) obj.status=2;
			}
		}
	}
	window.dejson=function(str,brief){
		try{return brief?eval("window.t="+str):JSON.parse(str);}catch(e){}
		return null;
	}
	window.enjson=function(obj){
		try{return JSON.stringify(obj)}catch(e){}
		return null;
	}
	Math.distance=function(x1,y1,x2,y2){
		return Math.sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));
	}
	Me.start=function(){
		window.Shell=null;
		if(window.require){
			window.Shell=require("electron");
			Shell.fs=require("fs");
			Shell.path=Shell.remote.app.getAppPath().replace(/\\/g,"/")+"/";
			Shell.ipcRenderer.send("focus");
		}
		Me.config=dejson(Me.readFile("js/config.txt?"+(new Date().valueOf())),true)||{};
		if(!Me.config.version) Me.config.version="1.0";
		window.onerror=Me.onError;
		if(Shell){
			eval(Me.readFile("js/dream-build.js"));
			if(!window.QRCode){
				eval(Me.readFile("js/dream-qrcode.js"));
			}
			eval(Me.readFile("js/dream-respack.js"));
			Me.code=Me.readFile("js/dream-code.js");
		}
		else{
			var m=unescape(document.URL).match(/\bdev=(\w+)/);
			var dev=!m?"":"."+m[1];
			Me.code=Me.readFile("js/dream-code"+dev+".js?"+Me.config.version);
		}
		eval(Me.code);
		for(var i=0;i<Class.items.length;i++){
			var classObj=Class.items[i];
			for(var k in classObj){
				var v=classObj[k];
				if(k.charAt(0)=="$"&&is(Function,v)){
					var s=v.toString().replace(/\r/g,"");
					classObj[k]=s.slice(s.indexOf("\n")+1,-3);
				}
			}
		}
		if(window.Respack){
			Respack.start();
		}
		else{
			G.start();
		}
	}
	Me.readFile=function(path){
		var xh = new XMLHttpRequest();
		xh.open("GET", path, false);
		try{
			xh.send(null);
			return xh.responseText;
		}
		catch(e){
			return "";
		}
	}
	Me.onError=function(msg,url,row,col){
		var className;
		var n=msg.indexOf(":");
		if(n>-1) msg=msg.slice(n+1);
		var pos=0;
		var line=0;
		for(var i=0;i<row;i++){
			var tmpPos=pos;
			pos=Me.code.indexOf("\n",tmpPos)+1;
			var s=Me.code.slice(tmpPos,pos);
			if(s.indexOf("=Class.extends(")>-1){
				line=0;
				var a=s.match(/(\w+)\",/);
				className=a[1];
			}
			line++;
		}
		var desc="Error in "+className+".js("+line+"):\n"+msg;
		if(!window.G||!window.G.stage){
			var div=document.createElement("div");
			document.body.appendChild(div);
			div.style.cssText="position:absolute;padding:10px;color:#d00000";
			div.innerText=desc;
		}
		else{
			trace(desc);
		}
	}
})();
(function(){
	window.Class=arguments.callee;
	Class.poolHash={};
	Class.items=[];
	Class.newInstanceId=1;
	Class.birthDiv=null;
	Class.extends=function(classPath,classSuper){
		var classObj=new Function();
		var className=classPath.replace(/[^\.]+\./g,"");
		window[className]=classObj;
		Class.items.push(classObj);
		classObj.__classPath=classPath;
		classObj.__className=className;
		classObj.__isClass=true;
		classObj.__super=classSuper;
		classObj.proto={};
		return classObj;
	}
	Class.new=function(classObj){
		var pools=Class.poolHash[classObj.__className];
		if(pools&&pools.length>0){
			var instance=pools.shift();
			for(var k in instance){
				if(k.indexOf("__")==0||k=="toString") continue;
				if(!(instance[k] instanceof Function)){
					delete instance[k];
				}
			}
		}
		else{
			instance=new classObj();
			instance.__id=Class.newInstanceId++;
			instance.__class=classObj;
			instance.__className=classObj.__className;
			instance.toString=function(){
				var caller=this;
				if(caller.tagName=="DIV"&&caller.box) caller=this.box;
				return "[object "+caller.__className+(caller.name?" "+caller.name:"")+"]";
			}
		}
		if(!classObj.__protos){
			classObj.__protos=[];
			var classObj2=classObj;
			while(classObj2){
				var protoArr=[];
				for(var k in classObj2.proto){
					var v=classObj2.proto[k];
					var clone=typeof(v)=="object"&&v;
					protoArr.push(k,clone?JSON.stringify(v):v,clone);
					if(k=="__ctor") protoArr.__ctor=v;
				}
				classObj.__protos.unshift(protoArr);
				classObj2=classObj2.__super;
			}
		}
		var protos=classObj.__protos;
		for(var i=0;i<protos.length;i++){
			var protoArr=protos[i];
			for(var j=0;j<protoArr.length;j+=3){
				var v=protoArr[j+1];
				if(protoArr[j+2]) v=JSON.parse(v);
				instance[protoArr[j]]=v;
			}
		}
		var args=arguments.length>1?[]:null;
		for(var i=1;i<arguments.length;i++){
			args.push(arguments[i]);
		}
		for(var i=0;i<protos.length;i++){
			var protoArr=protos[i];
			if(!protoArr.__ctor) continue;
			protoArr.__ctor.apply(instance,args);
		}
		return instance;
	}
})();
System.start();
